package com.wb.adutils.ads.pangle

import android.app.Activity
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.*
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants
import com.wb.adutils.tools.LogC
import java.lang.Exception


class PangleInterstitialAdUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity)  {

    private var mInterstitialFullAd: TTFullScreenVideoAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            if (isAdLoadSuccess) {
                listener?.onAdLoaded(null)
                return
            }

            isAdLoadSuccess = false
            LogC.e("pangle -> 插屏 -> 开始请求插屏广告")
            // 重置标签
            val mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity)
            val adSlot = AdSlot.Builder()
                .setCodeId(adEntity?.adUnit)
                .setSupportDeepLink(true)
                .setOrientation(orientation) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .setAdLoadType(TTAdLoadType.LOAD) //推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build()
            mTTAdNative.loadFullScreenVideoAd(adSlot, object : TTAdNative.FullScreenVideoAdListener {
                override fun onError(p0: Int, p1: String?) {
                    LogC.e("pangle -> 插屏 -> onInterstitialFullLoadFail: $p1")
                    excAdLoadFail(p0)
                }

                override fun onFullScreenVideoAdLoad(mttFullVideoAd: TTFullScreenVideoAd?) {
                    LogC.e("pangle -> 插屏 -> onFullScreenVideoAdLoad...")
                }

                override fun onFullScreenVideoCached() {
                    LogC.e("pangle -> 插屏 -> onFullScreenVideoCached...")
                }

                override fun onFullScreenVideoCached(mttFullVideoAd: TTFullScreenVideoAd?) {
                    LogC.e("pangle -> 插屏 -> onFullScreenVideoCached111..." + (mttFullVideoAd == null))
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("pangle -> 插屏 -> onInterstitialFullCached...但是已超时")
                        return
                    }
                    if (mttFullVideoAd == null) {
                        excAdLoadFail(AdConstants.AD_ERROR_CODE_INTERNAL)
                    } else {
                        LogC.e("pangle -> 插屏 -> onInterstitialFullCached")
                        timeoutDisposable?.dispose()
                        isAdLoadSuccess = true
                        mInterstitialFullAd = mttFullVideoAd
                        listener?.onAdLoaded(null)
                    }
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun showAd(adRoot: ViewGroup?) {
        try {
            LogC.e("pangle -> 插屏 -> showAd...")
            val activity = getActivity()
            if (activity == null) {
                release()
                return
            }
            if (isAdLoadSuccess) {
                //设置监听器
                mInterstitialFullAd?.setFullScreenVideoAdInteractionListener(object : TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
                    //广告的展示回调
                    override fun onAdShow() {
                        LogC.e("pangle -> 插屏 -> showAd -> onInterstitialFullShow...")
                        listener?.onAdDisplay()
                    }

                    //广告的下载bar点击回调
                    override fun onAdVideoBarClick() {
                        LogC.e("pangle -> 插屏 -> showAd -> onInterstitialFullClick...")
                        listener?.onAdClicked()
                    }

                    //广告关闭的回调
                    override fun onAdClose() {
                        LogC.e("pangle -> 插屏 -> showAd -> onInterstitialFullClosed...")
                        listener?.onAdComplete(true)
                        release()
                    }

                    //视频播放完毕的回调
                    override fun onVideoComplete() {
                        LogC.e("pangle -> 插屏 -> onVideoComplete")
                    }

                    //跳过视频播放
                    override fun onSkippedVideo() {
                        LogC.e("pangle -> 插屏 -> showAd -> onSkippedVideo...")
                    }
                })
                mInterstitialFullAd?.showFullScreenVideoAd(activity)
            } else {
                LogC.e("pangle -> 插屏 -> showAd ->  没有准备好")
                listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
                release()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun release() {
        super.release()
        mInterstitialFullAd = null
    }


}